package com.hundsun.cprs.yyt.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;


public class MoneyFenShrinkUtil {

    /**
     * 根据传入的计价单位(EnumValuationUnit 中定义)换算以分传入的金额
     * 返回换算后的金额以及单位结合字符串
     * @param money 分为计量单位的值
     * @param unit 取值于EnumValuationUnit.xx.getValue()
     * @return
     */
    public static String getValuationUnitDesc(Long money, String unit) {
        EnumValuationUnit enumUnit = EnumValuationUnit.indexByValue(unit);
        if (null == enumUnit || null == money) {
            return "";
        }
        BigDecimal _money = new BigDecimal(money);
        BigDecimal _rate = new BigDecimal(enumUnit.getRate());
        BigDecimal bd = _money.divide(_rate);
        bd = bd.setScale(enumUnit.getScale(), BigDecimal.ROUND_HALF_UP);
        StringBuffer formatString = new StringBuffer();
        formatString.append("0");
        if (enumUnit.getScale() > 0) {
            formatString.append(".");
            for (int i = 0; i < enumUnit.getScale(); i++) {
                formatString.append("#");
            }
        }
        DecimalFormat format = new DecimalFormat(formatString.toString());
        return format.format(bd) + enumUnit.getName();
    }
    
    /**
     * 根据以分为单位的金额,和现实单位,返回现实该单位的数字,有可能是带小数的,例如 27000000分  返回27万
     * @param money eg:以分计算
     * @param unit  eg:wangyuan
     * @return
     */
    public static String getValuationUnit(Long money, String unit) {
        EnumValuationUnit enumUnit = EnumValuationUnit.indexByValue(unit);
        if (null == enumUnit || null == money) {
            return "";
        }
        BigDecimal _money = new BigDecimal(money);
        BigDecimal _rate = new BigDecimal(enumUnit.getRate());
        BigDecimal bd = _money.divide(_rate);
        bd = bd.setScale(enumUnit.getScale(), BigDecimal.ROUND_HALF_UP);
        StringBuffer formatString = new StringBuffer();
        formatString.append("0");
        if (enumUnit.getScale() > 0) {
            formatString.append(".");
            for (int i = 0; i < enumUnit.getScale(); i++) {
                formatString.append("#");
            }
        }
        DecimalFormat format = new DecimalFormat(formatString.toString());
        return format.format(bd);
    }
    
    /**
     * 根据现实金额,和现实单位,返回以分为单位的数字,例如 1000  返回100000
     * @param money eg:1000
     * @param unit  eg:yuan
     * @return
     */
    public static String getAmplifyValuationUnit(String money, String unit) {
        EnumValuationUnit enumUnit = EnumValuationUnit.indexByValue(unit);
        if (null == enumUnit || null == money) {
            return "";
        }
        BigDecimal _money = new BigDecimal(money);
        BigDecimal _rate = new BigDecimal(enumUnit.getRate());
        BigDecimal bd = _money.multiply(_rate);
        bd = bd.setScale(enumUnit.getScale(), BigDecimal.ROUND_HALF_UP);
        StringBuffer formatString = new StringBuffer();
        formatString.append("0");
        if (enumUnit.getScale() > 0) {
            formatString.append(".");
            for (int i = 0; i < enumUnit.getScale(); i++) {
                formatString.append("#");
            }
        }
        DecimalFormat format = new DecimalFormat(formatString.toString());
        return format.format(bd);
    }
    
    /**
     * 
     * 方法说明：违约金显示控制（-52.3元显示“赔偿52.3元”，52.3元显示“补偿”）
     * @email xiejh@hundsun.com
     * add by xiejh 2011-12-1 下午01:46:25
     * @param money
     * @param unit
     * @return
     */
    public static String getPenaltyAmountDesc(Long money, String unit) {
        EnumValuationUnit enumUnit = EnumValuationUnit.indexByValue(unit);
        if (null == enumUnit || null == money) {
            return "";
        }
        BigDecimal _money = new BigDecimal(money);
        BigDecimal _rate = new BigDecimal(enumUnit.getRate());
        String amount_preString = "";
        //加前缀
        if(_money.longValue() > 0){
            amount_preString = "收入";
        }else{
            amount_preString = "支付";
        }
        BigDecimal bd = _money.divide(_rate);
        bd = bd.setScale(enumUnit.getScale(), BigDecimal.ROUND_HALF_UP);
        StringBuffer formatString = new StringBuffer();
        formatString.append("0");
        if (enumUnit.getScale() > 0) {
            formatString.append(".");
            for (int i = 0; i < enumUnit.getScale(); i++) {
                formatString.append("#");
            }
        }
        DecimalFormat format = new DecimalFormat(formatString.toString());
        return amount_preString+format.format(bd).replace("-", "") + enumUnit.getName();
    }
}
